﻿''' <summary>
''' This is a game component that implements IUpdateable.
''' </summary>

Public Class Player
    Inherits Microsoft.Xna.Framework.GameComponent
    Public _GameManager As GameManagerCollection

    Public Location As New Vector3(1, 100, 100)
    Public MoveSpeed As Single = 90.0F
    Public Angle As System.Nullable(Of Integer) = 0
    Public Sprite As Sprite
    Public Stats As Stats

    Public Sub New(ByVal Game As DanoQuest, Name As String, Level As Integer, Damage As Integer)
        MyBase.New(Game)
        Me.Game.Components.Add(Me)
        _GameManager = Game.GameManager
        Sprite = New Sprite(Name, New Stats With {.Level = Level, .Damage = Damage})
        Game.Components.OfType(Of SpriteManager).ToList.Item(0).AddPlayer(Sprite) 'Adding Player to SpriteManager
        Sprite.Target = Sprite
    End Sub

    ''' <summary>
    ''' Allows the game component to perform any initialization it needs to before starting
    ''' to run.  This is where it can query for any required services and load content.
    ''' </summary>
    Public Overrides Sub Initialize()
        ' TODO: Add your initialization code here
        MyBase.Initialize()
    End Sub

    ''' <summary>
    ''' Allows the game component to update itself.
    ''' </summary>
    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
    Public Overrides Sub Update(ByVal gameTime As GameTime)
        ' TODO: Add your update code here
        MyBase.Update(gameTime)
    End Sub

    Public Sub UpdatePosition(vectorToAdd As Vector3)
        Dim RotatedVector As Vector3 = Vector3.Transform(vectorToAdd, Matrix.CreateRotationY(_GameManager.Camera.HorizontalRotation)) '* Matrix.CreateRotationX(VerticalRotation) 'Rotates camera then adds direction
        Dim VerticalCollision As Single = _GameManager.CollisionManager.Distance(New Ray(Location + (MoveSpeed * RotatedVector), Vector3.Down), _GameManager.ThreeDElements.Models(0), _GameManager.ThreeDElements.ModelWorldTransforms(0), Angle)
        If _GameManager.ThreeDElements.TrianglePicking Then
            _GameManager.CollisionManager.Distance(New Ray(Location, Vector3.Down), _GameManager.ThreeDElements.Models(0), _GameManager.ThreeDElements.ModelWorldTransforms(0), Nothing, _GameManager.ThreeDElements.PickedTriangle)
        End If
        If VerticalCollision <= 10 Then  'Collision Check
            If Angle < 65 Then
                Location.Y += 10 - VerticalCollision
            Else
                MoveSpeed = 0.0F
            End If
        ElseIf VerticalCollision > 10 Then 'Gravity
            If Angle > 66 Then
                MoveSpeed = 0.0F
            Else
                Location.Y -= VerticalCollision - 10
            End If
        End If
        Location += movespeed * RotatedVector
        _GameManager.Camera.UpdateViewMatrix()
        MoveSpeed = 90.0F ' Resetting MoveSpeed!

    End Sub

End Class
